package com.xiaochen.product.controller;

import com.xiaochen.model.entity.product.Category;
import com.xiaochen.model.entity.product.ProductSku;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import com.xiaochen.model.vo.h5.IndexVo;
import com.xiaochen.product.service.CategoryService;
import com.xiaochen.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
//@CrossOrigin // (不推荐使用,但可以临时使用,后面使用网关解决)解决跨域问题
@RestController
@RequestMapping("/api/product/index")
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    @GetMapping
    public Result index(){
        log.info("查询首页信息");
        // 1.查询所有以及分类
        List<Category> categoryList =categoryService.selectOneCategory();
        // 2.根据销量查询前10条数据
        List<ProductSku> productSkuList = productService.selectHotProductSkuBySal();
        // 3.封装数据到IndexVo对象中
        IndexVo indexVo = new IndexVo();
        indexVo.setCategoryList(categoryList);
        indexVo.setProductSkuList(productSkuList);

        return Result.build(indexVo, ResultCodeEnum.SUCCESS);
    }

}
